﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Web.Mvc;
using NUnit.Framework;
using VictorBlaga.DocumentManagement.Controllers;
using VictorBlaga.DocumentManagement.Core.Models.Repositories;
using VictorBlaga.DocumentManagement.Core.Services;
using VictorBlaga.DocumentManagement.Helpers.Mappers;
using VictorBlaga.DocumentManagement.Helpers.Validators;
using VictorBlaga.DocumentManagement.Infrastructure.DataAccess;

namespace VictorBlaga.DocumentManagement.Tests.Controllers
{
    [TestFixture]
    public class AdministrationControllerTests
    {
        #region Dependecies
        private ICryptographerService _cryptographerService;
        private IUserMapper _userMapper;
        private IUserValidator _userValidator;
        private ITeamValidator _teamValidator;
        private IUserRepository _userRepository;
        private IWorkshopRepository _workshopRepository;
        private ITeamRepository _teamRepository;
        private ITeamMapper _teamMapper;
        private IWorkshopMapper _workshopMapper;
        private IWorkshopValidator _workshopValidator;
        #endregion

        private AdministrationController _adminController;
        private AdministrationMockRepository _administrationMockRepository;

        #region SetUp
        [SetUp]
        public void SetUpFixture()
        {
            _administrationMockRepository = new AdministrationMockRepository();
            SetUpDependencies();
            InstantiateController();
        }

        private void InstantiateController()
        {
            _adminController = new AdministrationController(_userRepository, _workshopRepository, _teamRepository,
                                                           _userMapper, _workshopMapper, _teamMapper,
                                                           _userValidator, _cryptographerService, _teamValidator, _workshopValidator);
        }

        private void SetUpDependencies()
        {
            _userRepository = _administrationMockRepository.GetUserRepository();
            _workshopRepository = _administrationMockRepository.GetWorkshopRepository();
            _teamRepository = _administrationMockRepository.GetTeamRepository();
            _cryptographerService = _administrationMockRepository.GetCryptographerService();
            _userMapper = _administrationMockRepository.GetUserMapper();
            _userValidator = _administrationMockRepository.GetUserValidator(true);
            _workshopMapper = _administrationMockRepository.GetWokshopMapper();
            _teamMapper = _administrationMockRepository.GetTeamMapper();
            _teamValidator = _administrationMockRepository.GetTeamValidator(true);
            _workshopValidator = _administrationMockRepository.GetWorkshopValidator(true);
        }

        #endregion

        [Test]
        public void CreateUserGetShouldDisplayForm()
        {
            
            var result = _adminController.CreateUser();

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(ViewResult), result);
        }

        [Test]
        public void CreateUserPostShouldRedisplayViewIfUserInvalid()
        {
            //By default the validator returns true
            //Get a false result by remocking the validator
            //And reinstantiating the controller
            _userValidator = _administrationMockRepository.GetUserValidator(false);
            InstantiateController();

            var result = _adminController.CreateUser(_administrationMockRepository.UserViewModel);
            Assert.IsInstanceOf(typeof(ViewResult), result);
        }

        [Test]
        public void CreateUserPostShouldRedirectToIndexIfUserValid()
        {
            var result = _adminController.CreateUser(_administrationMockRepository.UserViewModel);
            Assert.IsInstanceOf(typeof(RedirectToRouteResult), result);
        }
    }
}
